package com.android.danaobank.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.android.danaobank.R;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * <p>
 * Created by ligang on 2020/8/23 0023 10:39
 * ================================================
 */
public class RatingBar extends LinearLayout {
    private int starCount; //总星星数量
    private List<ImageView> starList = new ArrayList<>();
    private float rating; //初始化星星数
    private int starWidth; //星星宽度
    private int starHeight; //星星高度
    private int starMargin; //星星之间的间隔
    private Drawable starFullDrawable; //满星星
    private Drawable starHalfDrawable; //半星星
    private Drawable starEmptyDrawable; //空星星

    public RatingBar(Context context) {
        super(context);
        init(context, null);
    }

    public RatingBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RatingBar);
            rating = array.getInteger(R.styleable.RatingBar_star_rating, 3);
            starCount = array.getInteger(R.styleable.RatingBar_star_count, 5);
            starWidth = array.getDimensionPixelSize(R.styleable.RatingBar_star_width, 0);
            starMargin = array.getDimensionPixelSize(R.styleable.RatingBar_star_margin, 0);
            starFullDrawable = array.getDrawable(R.styleable.RatingBar_star_full_drawable);
            starHalfDrawable = array.getDrawable(R.styleable.RatingBar_star_half_drawable);
            starEmptyDrawable = array.getDrawable(R.styleable.RatingBar_star_empty_drawable);
            if (starHalfDrawable == null) {
                starHalfDrawable = starFullDrawable;
            }
            array.recycle();
        }

        for (int i = 0; i < starCount; i++) {
            ImageView star = new ImageView(context);
            LinearLayout.LayoutParams params;
            if (starWidth != 0) {
                params = new LinearLayout.LayoutParams(starWidth, LayoutParams.WRAP_CONTENT);
            } else {
                params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            }

            if (i != 0) {
                params.leftMargin = starMargin;
            }
            if (rating > i) {
                star.setImageDrawable(starFullDrawable);
            } else {
                star.setImageDrawable(starEmptyDrawable);
            }
            starList.add(star);
            addView(star, params);
        }
    }

    private float lastRating;

    private void updateStars(float rating) {
        int rateingInt = (int) rating;
        if (rateingInt == (int) lastRating) {
            if (rating - rateingInt >= 0.5f && lastRating - (int) lastRating >= 0.5f) {
                return;
            }
            if (rating - rateingInt < 0.5f && lastRating - (int) lastRating < 0.5f) {
                return;
            }
        }

        for (int i = 0; i < starCount; i++) {
            if (rateingInt > i) {
                starList.get(i).setImageDrawable(starFullDrawable);
            } else {
                starList.get(i).setImageDrawable(starEmptyDrawable);
            }
        }
        if (rateingInt < starCount) {
            if (rating - rateingInt >= 0.5f) {
                starList.get(rateingInt).setImageDrawable(starFullDrawable);
            } else if (rating - rateingInt > 0) {
                starList.get(rateingInt).setImageDrawable(starHalfDrawable);
            }
        }
        lastRating = rating;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float x = event.getX();
                // 星星 星星 星星 星星 星星
                // 20 10 20 10 20 10 20 10
                x = x - getPaddingLeft();
                int index = (int) (x / (starWidth + starMargin));

                float offset = x % (starWidth + starMargin);

                float decimal = offset / starWidth;

                rating = index + decimal;
                updateStars(rating);
                break;
        }
        return true;
    }
}
